//
//  GrantBillListController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/7/25.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "GrantBillListController.h"
#import "GrantBillListCell.h"
#import "GrantListContentModel.h"
#import "BalanceModel.h"
#import "IssueNowNoMoneyAlertController.h"
#import "IssueNowAlertController.h"
#import "ShenHeFinishedViewController.h"
@interface GrantBillListController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) BalanceModel *balanceModel;

@end

@implementation GrantBillListController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.page = 1;
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [self requestListData];
}
- (void)setupUI{
    self.title = @"发放补助金账单";
    self.view.backgroundColor = Main_BackGroundColor;
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.tableView.rowHeight = 180;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestListData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}
-(void)requestListData {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNo"] = @1;
    param[@"transfer"] = @"2";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:getGrantsList param:param success:^(id dic) {

        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.dataArray = [GrantListContentModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        self.page = 2;
        self.dataArray = [self.dataArray mutableCopy];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        [self.tableView reloadData];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];

    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
- (void)loadMoreData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNo"] = @(self.page);
    param[@"transfer"] = @"2";
    [NetWorkTool POST:getGrantsList param:param success:^(id dic) {
        NSMutableArray *modelArray = [GrantListContentModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        if (modelArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        self.page++;
        [self.dataArray addObjectsFromArray:modelArray];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } other:^(id dic) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GrantListContentModel * model = self.dataArray[indexPath.row];
    GrantBillListCell *cell = [GrantBillListCell cellWithTableView:tableView];
    [cell.IssueNowButton addTarget:self action:@selector(IssueNowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.IssueNowButton.tag = indexPath.row;
    cell.model = model;
    return cell;
}
- (void)IssueNowButtonClick:(UIButton *)sender{
    GrantListContentModel * model = self.dataArray[sender.tag];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = model.id;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:factoryAuditGrants param:param success:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.balanceModel = [BalanceModel mj_objectWithKeyValues:dic[@"data"]];
    
        if ([self.balanceModel.type isEqualToString:@"0"]) {//1可以支付 0余额不足
            // 余额不足
            IssueNowNoMoneyAlertController * vc = [[IssueNowNoMoneyAlertController alloc] init];
            vc.model = self.balanceModel;
            [self presentViewController:vc animated:YES completion:nil];
        } else {
            IssueNowAlertController * vc = [[IssueNowAlertController alloc] init];
            vc.model = self.balanceModel;
            vc.idStr = model.id;
            vc.okButtonClick = ^{
                [self.dataArray removeObject:model];
                [self.tableView reloadData];
            };
            [self presentViewController:vc animated:YES completion:nil];
        }
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GrantListContentModel * model = self.dataArray[indexPath.row];
    ShenHeFinishedViewController *vc = [[ShenHeFinishedViewController alloc]init];
    vc.idStr = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
